package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/8/6 21:16
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> getPage(DishPageQueryDTO dishPageQueryDTO){
        return Result.success(dishService.getPage(dishPageQueryDTO));
    }


    /**
     * 菜品添加
     * @param dishDTO
     */
    @ApiOperation("菜品添加")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("Saving dish:{}", dishDTO);
        dishService.save(dishDTO);
        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 根据id进行菜品的删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id进行菜品的删除")
    public Result deleteDish(Long[] ids){
        //将所有的菜品缓存清除
        cleanCache("dish_*");
        return dishService.deleteDish(ids);
    }

    /**
     * 根据id进行菜品信息查询
     * @param id
     * @return
     */
    @ApiOperation("根据id进行菜品信息查询")
    @GetMapping("/{id}")
    public Result<DishDTO> selectById(@PathVariable("id") Long id){
        return Result.success(dishService.selectById(id));
    }

    /**
     * 根据id修改菜品信息
     * @param dishDTO
     * @return
     */
    @ApiOperation(("根据id修改菜品信息"))
    @PutMapping
    public Result updateById(@RequestBody DishDTO dishDTO){
        log.info("dishDTO:{}", dishDTO);
        dishService.updateById(dishDTO);
        //将所有的菜品缓存清除
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据菜品id进行状态修改
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("根据菜品id进行状态修改")
    public Result updateDishStatus(Long id,@PathVariable("status")Integer status){
        Result result = dishService.updateDishStatus(id, status);
        //将所有的菜品缓存清除
        cleanCache("dish_*");
        return result;
    }

    /**
     * 根据分类查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation(("根据分类查询菜品"))
    @GetMapping("/list")
    public Result<List<Dish>> selectByCategoryId(Long categoryId){
        return Result.success(dishService.selectByCategoryId(categoryId));
    }



    private void cleanCache(String patten){
        Set keys = redisTemplate.keys(patten);
        redisTemplate.delete(keys);
    }







}
